package org.jeecg.modules.wms.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wms.entity.WmsSupplier;
import org.jeecg.modules.wms.entity.WmsSupplierContact;
import org.jeecg.modules.wms.service.IWmsSupplierContactService;
import org.jeecg.modules.wms.service.IWmsSupplierService;
import org.jeecg.modules.wms.vo.WmsPurchaseOrderVO;
import org.jeecg.modules.wms.vo.WmsSupplierVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 供应商管理
 * @Author: jeecg-boot
 * @Date: 2023-07-18
 * @Version: V1.0
 */
@Api(tags="供应商管理")
@RestController
@RequestMapping("/wms/supplier")
@Slf4j
public class WmsSupplierController extends JeecgController<WmsSupplier, IWmsSupplierService> {
    @Autowired
    private IWmsSupplierService wmsSupplierService;
    
    @Autowired
    private IWmsSupplierContactService wmsSupplierContactService;
    
    /**
     * 分页列表查询
     */
    @ApiOperation(value="供应商列表", notes="获取供应商列表")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WmsSupplier wmsSupplier,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<WmsSupplier> queryWrapper = QueryGenerator.initQueryWrapper(wmsSupplier, req.getParameterMap());
        Page<WmsSupplier> page = new Page<WmsSupplier>(pageNo, pageSize);
        IPage<WmsSupplier> pageList = wmsSupplierService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    
    /**
     * 添加
     */
    @ApiOperation(value="添加供应商", notes="添加供应商")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WmsSupplierVO wmsSupplierVO) {
        try {
            // 保存供应商基本信息
            wmsSupplierService.saveSupplier(wmsSupplierVO);
            
            return Result.OK("添加成功！");
        } catch (Exception e) {
            log.error("添加供应商失败", e);
            return Result.error("添加失败: " + e.getMessage());
        }
    }
    
    /**
     * 编辑
     */
    @ApiOperation(value="编辑供应商", notes="编辑供应商")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WmsSupplierVO wmsSupplierVO) {
        wmsSupplierService.updateSupplier(wmsSupplierVO);
        return Result.OK("编辑成功!");
    }
    
    /**
     * 通过id删除
     */
    @ApiOperation(value="删除供应商", notes="删除供应商")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id") String id) {
        wmsSupplierService.deleteSupplier(id);
        return Result.OK("删除成功!");
    }
    
    /**
     * 批量删除
     */
    @ApiOperation(value="批量删除供应商", notes="批量删除供应商")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids") String ids) {
        this.wmsSupplierService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     */
    @ApiOperation(value="供应商详情", notes="供应商详情")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id") String id) {
        WmsSupplierVO wmsSupplierVO = wmsSupplierService.getSupplierById(id);
        if(wmsSupplierVO==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsSupplierVO);
    }
    
    /**
     * 导出excel
     */
    @AutoLog(value = "供应商-导出")
    @ApiOperation(value="供应商-导出", notes="供应商-导出")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsSupplier wmsSupplier) {
        return super.exportXls(request, wmsSupplier, WmsSupplier.class, "供应商数据");
    }
    
    /**
     * 通过excel导入数据
     */
    @AutoLog(value = "供应商-导入")
    @ApiOperation(value="供应商-导入", notes="供应商-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WmsSupplier.class);
    }
    
    /**
     * 获取供应商采购记录
     */
    @ApiOperation(value="获取供应商采购记录", notes="根据供应商ID获取历史采购记录")
    @GetMapping(value = "/purchase/history")
    public Result<?> getSupplierPurchaseHistory(
        @RequestParam(name="supplierId") String supplierId,
        @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<WmsPurchaseOrderVO> page = new Page<>(pageNo, pageSize);
        IPage<WmsPurchaseOrderVO> pageList = wmsSupplierService.getSupplierPurchaseHistory(supplierId, page);
        return Result.OK(pageList);
    }
} 